package com.ruoyi.yys.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.yys.util.dbUtil.JdbcDbUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yys.domain.JdbcConfig;
import com.ruoyi.yys.service.IJdbcConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据库连接配置Controller
 * 
 * @author xinyin
 * @date 2022-12-07
 */
@RestController
@RequestMapping("/importExport/jdbcConfig")
public class JdbcConfigController extends BaseController
{
    @Autowired
    private IJdbcConfigService jdbcConfigService;

    /**
     * 查询数据库连接配置列表
     */
    @PreAuthorize("@ss.hasPermi('importExport:jdbcConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(JdbcConfig jdbcConfig)
    {
        startPage();
        List<JdbcConfig> list = jdbcConfigService.selectJdbcConfigList(jdbcConfig);
        return getDataTable(list);
    }

    /**
     * 导出数据库连接配置列表
     */
    @PreAuthorize("@ss.hasPermi('importExport:jdbcConfig:export')")
    @Log(title = "数据库连接配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JdbcConfig jdbcConfig)
    {
        List<JdbcConfig> list = jdbcConfigService.selectJdbcConfigList(jdbcConfig);
        ExcelUtil<JdbcConfig> util = new ExcelUtil<JdbcConfig>(JdbcConfig.class);
        util.exportExcel(response, list, "数据库连接配置数据");
    }

    /**
     * 获取数据库连接配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('importExport:jdbcConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(jdbcConfigService.selectJdbcConfigById(id));
    }

    /**
     * 新增数据库连接配置
     */
    @PreAuthorize("@ss.hasPermi('importExport:jdbcConfig:add')")
    @Log(title = "数据库连接配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JdbcConfig jdbcConfig)
    {
        return toAjax(jdbcConfigService.insertJdbcConfig(jdbcConfig));
    }

    /**
     * 修改数据库连接配置
     */
    @PreAuthorize("@ss.hasPermi('importExport:jdbcConfig:edit')")
    @Log(title = "数据库连接配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JdbcConfig jdbcConfig)
    {
        return toAjax(jdbcConfigService.updateJdbcConfig(jdbcConfig));
    }

    /**
     * 删除数据库连接配置
     */
    @PreAuthorize("@ss.hasPermi('importExport:jdbcConfig:remove')")
    @Log(title = "数据库连接配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(jdbcConfigService.deleteJdbcConfigByIds(ids));
    }

    /**
     * 测试连接
     *
     * @return {@link R}
     */
    @GetMapping("/testConnectById")
    public R testConnectById(String id) {
        try {
            JdbcConfig jdbcConfig = jdbcConfigService.selectJdbcConfigById(id);
            JdbcDbUtil.testConnection(jdbcConfig);
            return R.ok("连接成功");
        } catch (Exception e) {
            return R.fail("连接失败：" + e.getMessage());
        }
    }

    /**
     * 测试连接
     *
     * @return {@link R}
     */
    @GetMapping("/testConnectByConfig")
    public R testConnectByConfig(JdbcConfig jdbcConfig) {
        try {
            JdbcDbUtil.testConnection(jdbcConfig);
            return R.ok("连接成功");
        } catch (Exception e) {
            return R.fail("连接失败：" + e.getMessage());
        }
    }

    /**
     * 数据源列表
     *
     * @return {@link R}
     */
    @GetMapping("/listDatasource")
    public R listDatasource() {
        return R.ok(jdbcConfigService.listDatasource());
    }

    /**
     * 根据数据源获取该库的所有表
     *
     * @return {@link R}
     */
    @GetMapping("/listTables")
    public R listTables(String databaseId) {
        return R.ok(jdbcConfigService.listTables(databaseId));
    }
}
